﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Rally_like_a_madman.Data
{
    public class RallyDatabase : DataContext 
    {
        public Table<Waypoint> WaypointTable;
        public Table<Route> RouteTable;
        public Table<RouteWaypointLink> RouteLinkTable;
        public Table<Score> ScoreTable;
        public Table<User> UserTable;

        public RallyDatabase(string connection) : base(connection) 
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connection"></param>
        public static void MakeDB(string connection)
        {
            RallyDatabase db = new RallyDatabase(connection);

            //Temp
            db.DeleteDatabase();

            if (!db.DatabaseExists())
            {
                db.CreateDatabase();

                #region waypoint
                Waypoint w1 = new Waypoint();
                w1.Name = "VVV Breda";
                w1.GPSLatitude = 51.59380;
                w1.GPSLongitude = 4.77963;
                db.WaypointTable.InsertOnSubmit(w1);

                Waypoint w2 = new Waypoint();
                w2.Name = "Liefdeszuster";
                w2.GPSLatitude = 51.59307;
                w2.GPSLongitude = 4.77969;
                db.WaypointTable.InsertOnSubmit(w2);

                Waypoint w3 = new Waypoint();
                w3.Name = "Valkenberg";
                w3.GPSLatitude = 51.59250;
                w3.GPSLongitude = 4.77969;
                db.WaypointTable.InsertOnSubmit(w3);
                #endregion

                #region route
                Route testRoute1 = new Route();
                testRoute1.Name = "Kroegentocht";
                db.RouteTable.InsertOnSubmit(testRoute1);

                Route testRoute2 = new Route();
                testRoute2.Name = "Bezienswaardighedentocht";
                db.RouteTable.InsertOnSubmit(testRoute2);
                #endregion

                #region waypointlinks
                RouteWaypointLink routeWaypointLink1 = new RouteWaypointLink();
                routeWaypointLink1.Route = testRoute1;
                routeWaypointLink1.Waypoint = w1;
                routeWaypointLink1.WaypointIndex = 0;
                db.RouteLinkTable.InsertOnSubmit(routeWaypointLink1);

                RouteWaypointLink routeWaypointLink2 = new RouteWaypointLink();
                routeWaypointLink2.Route = testRoute1;
                routeWaypointLink2.Waypoint = w2;
                routeWaypointLink2.WaypointIndex = 1;
                db.RouteLinkTable.InsertOnSubmit(routeWaypointLink2);

                RouteWaypointLink routeWaypointLink3 = new RouteWaypointLink();
                routeWaypointLink3.Route = testRoute1;
                routeWaypointLink3.Waypoint = w3;
                routeWaypointLink3.WaypointIndex = 2;
                db.RouteLinkTable.InsertOnSubmit(routeWaypointLink3);

                RouteWaypointLink routeWaypointLink4 = new RouteWaypointLink();
                routeWaypointLink4.Route = testRoute2;
                routeWaypointLink4.Waypoint = w1;
                routeWaypointLink4.WaypointIndex = 0;
                db.RouteLinkTable.InsertOnSubmit(routeWaypointLink4);

                RouteWaypointLink routeWaypointLink5 = new RouteWaypointLink();
                routeWaypointLink5.Route = testRoute2;
                routeWaypointLink5.Waypoint = w2;
                routeWaypointLink5.WaypointIndex = 1;
                db.RouteLinkTable.InsertOnSubmit(routeWaypointLink5);
                #endregion

                db.SubmitChanges();

                //Working now, testing! Print out the IDRouteWaypointLink of every RouteWaypointLink in the Table
                //var rwps = from RouteWaypointLink r in db.RouteLinkTable select r;

                //List<RouteWaypointLink> rwplist = rwps.ToList() as List<RouteWaypointLink>;

                //foreach (RouteWaypointLink r in rwplist)
                //{
                //    System.Diagnostics.Debug.WriteLine(r.IDRouteWaypointLink + " " + r.Route.Name + " " + r.Waypoint.Name + " " + r.WaypointIndex);
                //}
            }
        }
    }
}
